import Vue from 'vue'
import VueRouter from 'vue-router'

import routes from '@/router/routes'
import enhance from '@/utils/enchance'

//解决路径重复报错问题：(要处理失败的promise)
//备份
// const nativePush = VueRouter.prototype.push;
// const nativeReplace = VueRouter.prototype.push;

// //重写方法
//   /**
//    * Programmatically navigate to a new URL by pushing an entry in the history stack.
//    *
//    * @param to Route location to navigate to
//    * @param onComplete Navigation success callback
//    * @param onAbort Navigation aborted callback
//    */
// VueRouter.prototype.push =  function(to,onComplete,onAbort){
//   //具体为什么我不知道了
//   // if(onComplete === undefined && onAbort === undefined) {
//   //   return nativePush.call(this,to,onComplete,onAbort).catch(() => {})
//   // } else {
//   //   return nativePush.call(this,to,onComplete,onAbort)
//   // }
//   if (onAbort === undefined) {
//     //this指向调用者router
//     return nativePush.call(this,to,onComplete,()=>{})
//   } else {
//     return nativePush.call(this,to,onComplete,onAbort)
//   }
// }
// VueRouter.prototype.replace =  function(to,onComplete,onAbort){
//   if (onAbort === undefined) {
//     return nativeReplace.call(this,to,onComplete,()=>{})
//   } else {
//     return nativeReplace.call(this,to,onComplete,onAbort)
//   }
// }

enhance('push')
enhance('replace')
// console.log(VueRouter.prototype.push)


Vue.use(VueRouter)

const router = new VueRouter({
  routes,
  mode:'history'
})

export default router;